/*
* Copyright (C) Microsoft. All rights reserved.  
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not 
* use this file except in compliance with the License.  You may obtain a copy 
* of the License at http://www.apache.org/licenses/LICENSE-2.0  
* 
* THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED 
* WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, 
* MERCHANTABLITY OR NON-INFRINGEMENT. 
*
* See the Apache License, Version 2.0 for specific language governing 
* permissions and limitations under the License.
*
*/
#pragma once
#include <baja/transition_tracking/tracked_object.h>
#include <baja/simple_collection.hpp>
#include <dependencies/includes/ink_renderer_interface.h>
#include <journal/app/constants.h>
#include <journal/models/interfaces.h>
#include <journal/models/internal_interfaces.h>

namespace journal { namespace model {

namespace b = baja;
namespace s = baja::storage;
namespace m = baja::math;
namespace bt = baja::transition_tracking;
namespace bm = baja::model;
namespace bi = baja::ink;

class ink_stroke_model :
    public bt::tracked_object,
    public iink_stroke_model,
    public bi::iink_stroke_data,
    public iinternal_ink_stroke_model,
    public iinternal_model_blob,
    public std::enable_shared_from_this<ink_stroke_model>
{
public:
    // constuctor
    ink_stroke_model(
        const b::uint32& typeToken,
        const std::shared_ptr<iink_surface_model>& parent,
        const b::uint32 inkStrokeId
        );
    static std::shared_ptr<ink_stroke_model> create(
        const b::uint32& typeToken,
        const std::shared_ptr<iink_surface_model>& parent,
        const b::uint32 inkStrokeId);

    // iink_stroke_model
    b::uint32 storageId();
    std::shared_ptr<iink_surface_model> parent();

    std::shared_ptr<b::isimple_collection<bi::ink_point>> points();
    void addPoint(const m::point2d& position, const b::float32 pressure, const b::uint64 t);

    b::Boolean highlighter();
    bi::ink_color color();
    void setColor(const bi::ink_color color);

    b::float32 width();
    void setWidth(const b::float32 width);

    DEBUG_MODEL_DECLARATION;

    // iinternal_ink_stroke_model
    b::uint32 sizeInMemory();

    // iinternal_model_blob
    b::uint32 blobSize();
    void packToBlob(s::binary_blob& outBlob, b::uint32& offset);
    void unpackFromBlob(const s::binary_blob& inBlob, b::uint32& offset);

    // destructor
    ~ink_stroke_model();

private:

    // state
    std::recursive_mutex _mutex;

    // tree data structure support
    std::weak_ptr<iink_surface_model> _parent;

    // attributes
    b::uint64 _t0; // time when the stroke started, in ticks
    bi::ink_color _color;
    b::float32 _width;
    std::vector<bi::ink_point> _points;
    b::vector_collection<bi::ink_point> _pointCollection;

    // private methods
    b::uint32 ticksToMs(b::uint64 tTicks);
};

std::shared_ptr<iink_stroke_model> createInkStroke(
    const b::uint32& typeToken,
    const std::shared_ptr<iink_surface_model>& parent,
    const b::uint32 inkStrokeId);

}} // namespace journal::model